package com.yl.ydk.presenter.impl

import android.content.Context
import android.content.Intent
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.yl.ydk.R
import com.yl.ydk.api.Api
import com.yl.ydk.base.BaseFragmentPageAdapter
import com.yl.ydk.base.BasePresenter
import com.yl.ydk.bean.DiseaseCateBean
import com.yl.ydk.presenter.contract.DiseaseCateActivityContract
import com.yl.ydk.ui.activity.DiseaseCateActivity
import com.yl.ydk.ui.activity.DiseaseDetailActivity
import com.yl.ydk.ui.adapter.DiseaseAdapter
import com.yl.ydk.ui.fragment.DiseaseDetailFragment
import q.rorbin.verticaltablayout.VerticalTabLayout
import q.rorbin.verticaltablayout.adapter.TabAdapter
import q.rorbin.verticaltablayout.widget.ITabView
import q.rorbin.verticaltablayout.widget.ITabView.TabIcon
import q.rorbin.verticaltablayout.widget.ITabView.TabTitle
import rx.Observer
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import java.util.*


class DiseaseCateActivityImpl : BasePresenter<DiseaseCateActivityContract.View>(),
    DiseaseCateActivityContract.Presenter<DiseaseCateActivityContract.View> {


    private var mAdapter: BaseFragmentPageAdapter? = null
    private val mTitleList = ArrayList<String>()
    private val mFragments = ArrayList<Fragment>()
    private var diseaseAdapter: DiseaseAdapter? = null

    private val icons = arrayListOf<Int>(
        R.drawable.icon_disease_item1,
        R.drawable.icon_disease_item2,
        R.drawable.icon_disease_item3,
        R.drawable.icon_disease_item4,
        R.drawable.icon_disease_item5,
        R.drawable.icon_disease_item6,
        R.drawable.icon_disease_item7,
        R.drawable.icon_disease_item8,
        R.drawable.icon_disease_item9,
        R.drawable.icon_disease_item10,
        R.drawable.icon_disease_item11,
        R.drawable.icon_disease_item12,
        R.drawable.icon_disease_item13
    )


    override fun getHealthGDiseaseCate(context: Context, vararg query: String?) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getHealthGDiseaseCate(*query)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<DiseaseCateBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        mView?.showError(e.toString())
                    }

                    override fun onNext(t: DiseaseCateBean?) {
                        dismissLoadingDialog()
                        if (t?.data != null && t.code == 200) {

                            t.data[0].list.forEach {
                                mTitleList.add(it.name)
                                mFragments.add(DiseaseDetailFragment.getInstance(it.symptoms.toMutableList()))
                            }

                            mAdapter = BaseFragmentPageAdapter(
                                mView?.getActivity()?.supportFragmentManager,
                                mFragments, mTitleList
                            )

                            mView?.getViewPager()?.adapter = mAdapter
                            mView?.getVerticalTabLayout()?.setupWithViewPager(mView?.getViewPager())

                            mView?.getVerticalTabLayout()?.let {
                                setVerticalTabLayoutItem(it, context)
                            }

                            diseaseAdapter = DiseaseAdapter(t.data[0].diseases.toMutableList())
                            mView?.getRecyclerView()?.adapter = diseaseAdapter
                            val manager = LinearLayoutManager(context)
                            manager.orientation = LinearLayoutManager.HORIZONTAL
                            mView?.getRecyclerView()?.layoutManager = manager
                            diseaseAdapter?.onListItemClick(object :
                                DiseaseAdapter.onListItemClick {
                                override fun onListItemClick(item: DiseaseCateBean.Disease?) {
                                    DiseaseCateActivity.diseaseCateActivity?.let {
                                        val intent = Intent(it, DiseaseDetailActivity::class.java)
                                        intent.putExtra("data", item?.name)
                                        it.startActivityIn(intent, it)
                                    }
                                }
                            })

                        } else {
                            mView?.showError(context.getString(R.string.connectFailure))
                        }
                    }
                })
        )
    }

    private fun setVerticalTabLayoutItem(tabLayout: VerticalTabLayout, context: Context) {
        tabLayout.setTabAdapter(object : TabAdapter {
            override fun getCount(): Int {
                return mTitleList.size
            }

            override fun getBadge(position: Int): ITabView.TabBadge? {
                return null
            }

            override fun getIcon(position: Int): TabIcon? {
//                return TabIcon.Builder()
//                    .setIcon(icons[position], icons[position])
//                    .setIconSize(20,20)
//                    .build()
                return null
            }

            override fun getTitle(position: Int): TabTitle {
                return TabTitle.Builder()
                    .setContent(mTitleList[position]) //设置数据   也有设置字体颜色的方法
                    .setTextColor(
                        context.resources.getColor(R.color.white),
                        context.resources.getColor(R.color.white)
                    )
                    .setTextSize(20)
                    .build()
            }

            override fun getBackground(position: Int): Int {
                return 0 //选中的背景颜色
            }
        })
    }

}